const path = require('path')
// 引入后报错 1.降低版本；2. Cannot find module 'webpack/lib/node/NodeTemplatePlugin' 执行命令npm link webpack
const HtmlWebpackPlugin = require('html-webpack-plugin') 
const CopyPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// mini-css-extract-plugin

module.exports = {
  mode: process.env.NODE_ENV === "production"? "production":"development",

  devtool: 'source-map',

  // 配置入口
  entry: {
    // app: './src/app.js'
    'js/app': './src/app.js'
  },
  // 配置出口
  output: {
    // 物理路径
    path: path.resolve(__dirname, './dist'),
    // filename: 'app.js'
    // filename:  '[name]-[hash:6].js'
    filename:  '[name].js'
  },

  // loaders: {
    module: {
      rules: [
        // {
        //   test: /\.jpg$/,
        //   loader: "file-loader"
        // }, {
        //     test: /\.png$/,
        //     loader: "url-loader?mimetype=image/png"
        // }, 
        {
          // test: /\.art$/,
          // exclude: /(node_modules)/,
          // loader: "art-template-loader",
          test: /\.art$/,
          exclude: /(node_modules)/,
          use: {
            loader: "art-template-loader",
            options: {
              escape: false
            }
          }
          // options: {
          //     // art-template options (if necessary)
          //     // @see https://github.com/aui/art-template
          // }
        }, {
          test: /\.css$/,
          use: ['style-loader', 'css-loader'] // 
        },
        {
          test: /\.(png|jpg|gif)$/,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 8192
              }
            }
          ]
        }
      ]
    },
  // },

  // 配置插件
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({ // 这里需要传参，index.html的内容才能展示
      title: 'admin',
      template: path.join(__dirname, './public/index.html'),
      filename: 'index.html',
      // ,inject: 'head'
      inject: true
    }),
    new CopyPlugin({
      // { 
      //   from: path.join(__dirname, './public/*.ico'), 
      //   to: path.join(__dirname, './dist') 
      // }
      patterns: [{ // 版本问题，不能看webpack官网；注意from to路径
        from: './public/*.ico', 
        to: path.join(__dirname, './dist/favicon.ico') 
      }, {
        from: './public/libs',
        to: path.join(__dirname, './dist/libs') 
      }]
    })
  ],
  // 配置server
  devServer: {
    contentBase: path.join(__dirname, './dist'),
    compress: true, // 编译器
    port: 9000,
    proxy: {
      '/api': {
        target: "http://localhost:3000"
      }
    }
  }
}
